package com.example.demomybatis.controller;


import com.example.demomybatis.entity.Student;
import com.example.demomybatis.service.StudentService;
import com.example.demomybatis.vo.RequestStudentPr;
import com.example.demomybatis.vo.ResultStudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api")
public class StudentController {

    @Autowired
    StudentService studentService;

    @RequestMapping("/selectStudent")
    @ResponseBody
    public ResultStudentVo selectStudent(RequestStudentPr requestStudentPr){
        ResultStudentVo resultVo  = new ResultStudentVo();
        System.out.println("requestStudentPr.getPageNum()  ：  "+requestStudentPr.toString());
        if(requestStudentPr.getPageNum()!=null&&requestStudentPr.getPageSize()!=null)
         requestStudentPr.setPageNum((requestStudentPr.getPageNum()-1)*requestStudentPr.getPageSize());
        resultVo.setList(studentService.selectStudent(requestStudentPr));
        resultVo.setTotal(studentService.selectCountStudent(requestStudentPr));
        return resultVo;
    }


    @RequestMapping("/getOneStudent")
    @ResponseBody
    public Student getOneStudent(Integer studentId){

        return studentService.selectOneStudent(studentId);
    }


    @RequestMapping("/deleteStudentById")
    @ResponseBody
    public Integer deleteStudentById(Integer studentId){
        return studentService.deleteStudent(studentId);
    }


    @RequestMapping("/updateStudentById")
    @ResponseBody
    public Integer updateStudentById(Student student){
        return studentService.updateStudent(student);
    }

    @RequestMapping("/addStudentBy")
    @ResponseBody
    public Student addStudentBy(@RequestBody  Student student){
        return studentService.addStudent(student);
    }

  
}
